/**
 * 表单操作按钮
 * */
// ===================================================================== 
import React from 'react'
// ===================================================================== 
const { $http, $fn, $Lazy } = window
// ===================================================================== antd
const BtnGroup 	= $Lazy.load(()=>import('#tp/btn-group'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== 
const _ = {
	 edit: function(api, param, option){
		const opt = {
			idStr:'uuid',
			nameStr:'uuid',
			...option
		}
		let { select, ids, idx, click, load, idStr, nameStr, rows, echo, label, onValue, disabled, address, tabIndex } = opt
		return { label: label ? label : '编辑', disabled, tabIndex, onClick: async ()=>{
			// 打开弱窗
			this.modal?.open({ formRef:this.formRef })
			// 加载回显数据（如下拉列表数据）
			if(select){ await select() }
			
			// 请求数据前执行作用
			click?.()
			
			param = typeof param === 'function' ? param() : param
			
			if(api){
				$http.submit(null,api, { param, isPull:true }).then(async data =>{
					// 地区
					let areaData
					if(data.province_id){
						areaData = [ data.province_id, data.city_id, data.area_id ]
						if(address){
							await address(areaData)
						}
					}
					load?.(data)
					// 设置默认值
					if(onValue){
						onValue?.(data)
					}else{
						const echoValue = echo ? echo(data) : {...data, areaData}
						this.formRef?.setValue(echoValue)
					}
					// 动态获取参数
					if(ids){ ids = typeof ids === 'function' ? ids(data) : ids}
					if(idx){ idx = typeof idx === 'function' ? idx(data) : idx}
					
					//
					this.setState( { ids: idx ? idx : { [idStr]: data[nameStr], ...ids } } )
				})
				
			}else{
				this.formRef?.setValue(rows)
				load?.(rows)
				// 动态获取参数
				if(ids){ ids = typeof ids === 'function' ? ids(rows) : ids}
				this.setState( { ids:{ [idStr]: rows[nameStr], ...ids } } )
			}
		} }
	},
	// 点击
	click: function(label, click, option){
		const opt = {
			pushStr:'pushRoot',
			...option
		}
		const { pushStr } = opt
		return { label, ...option, onClick:()=> (typeof click === 'string' ? $fn[pushStr](this, click) : click()) }
	},
	// 仅跳转
	push: function(label, to, option){
		return _.click.call(this,label, to, { ghost:true, ...option})
	},
	pushCenter:function(label, to, option){
		return _.click.call(this,label, to, { ghost:true, pushStr:'pushCenter', ...option})
	},
	pushTo: function(label, to, option){
		return _.click.call(this,label, to, { ghost:true, pushStr:'pushTo', ...option})
	},
	// 操作按钮组件
	controls: function(option){
		return { title: '操作', ...$fn.table.controls, render:(text,rows) => <BtnGroup controls={this.table_controls(text, rows)} />, ...option}
	},
	// 上传
	upload: function(change, option){
		return { title:'上传', onChange: change,  ...option, type:'upload',  }
	},
	// 删除
	del: function(option){
		let { refresh = true, onOk } =  option
		return { title:'上传', ...option, type:'del', onOk: async close =>{
			if(refresh){
				await this?.fetch()
				$fn.clearKeys.call(this)
			}
			onOk?.(close)
		}}
	},
	// Switch
	Switch: function(api, param, option){
		return { loading: this.state.submitLoading, type:'switch', hide:this.state.hide, onChange: value=>{
			$http.submit(this,api, { param }).then(data=>{
				this.fetch().then(data=> {
					$fn.resetTemplate.call(this)
					message.then(f=>f.success('操作成功'))
				} )
			})
		}, ...option }
	}
}
export default _